package com.project.solomon.MBean;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.http.HttpServletRequest;

import com.project.entity.Account;
import com.project.entity.PropForRent;
import com.project.entity.PropForSale;
import com.project.entity.RoomForRent;

import com.project.entity.Property;

import com.project.solomon.SessionBean.PropForRentBeanRemote;
import com.project.solomon.SessionBean.PropForSaleBeanRemote;
import com.project.solomon.SessionBean.PropertyBeanRemote;
import com.project.solomon.SessionBean.RoomForRentBeanRemote;
import com.project.tructc.MBean.MPropertyImage;

/**
 * @author Solomon
 * 
 */

@ManagedBean(name = "mPropForRent")
@RequestScoped
public class AddPropertyBean {

	private Property prop;
	private Account currentAccount;

	private PropForRent propRent;
	private PropForSale propSale;
	private RoomForRent roomRent;

	private String genPID;
	private String genSID;
	private String genRID;

	private int totalProperty;
	private boolean publishability;

	private Date today;
	FacesContext context = FacesContext.getCurrentInstance();
	private boolean success = false;

	public AddPropertyBean() {
		prop = new Property();
		propRent = new PropForRent();
		propSale = new PropForSale();
		roomRent = new RoomForRent();
	}

	public String addNewPropForRent() throws Exception {
		try {

			HttpServletRequest req = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();
			MPropertyImage mPropImg = (MPropertyImage) req.getSession()
					.getAttribute("mPropertyImage");
			Context ctx = new InitialContext();
			PropForRentBeanRemote propRentRe = (PropForRentBeanRemote) ctx
					.lookup("MySweetHome/PropForRentBean/remote");

			propRent.setPropID(getGenPID());

			prop.setPropForRent(getPropRent());

			prop.setPropID(getGenPID());
			prop.setAccount(getCurrentAccount());
			prop.setPostDate(getToday());
			prop.setStatus(false);

			int imgList = mPropImg.insertImagesToList(getProp());

			if (imgList < 1) {
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_ERROR, "Publish denied",
						"You need to upload atleast one image!"));
				return "failed";
			}

			propRentRe.addNewPropForRent(getProp());
			propRentRe.addNewPropForRentImages(getProp(),
					mPropImg.getProp_img());
			mPropImg.setProp_img(new ArrayList());
			setSuccess(true);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Insert Successful!"));
			return "ok";

		} catch (Exception ex) {
			ex.printStackTrace();
			setSuccess(false);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Insert Failed!"));
			return "failed";
		}

	}

	public String addNewPropForSale() throws Exception {
		try {

			HttpServletRequest req = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();
			MPropertyImage mPropImg = (MPropertyImage) req.getSession()
					.getAttribute("mPropertyImage");
			Context ctx = new InitialContext();
			PropForSaleBeanRemote propSaleRe = (PropForSaleBeanRemote) ctx
					.lookup("MySweetHome/PropForSaleBean/remote");

			propSale.setPropID(getGenSID());

			prop.setPropForSale(getPropSale());

			prop.setPropID(getGenSID());
			prop.setAccount(getCurrentAccount());
			prop.setPostDate(getToday());
			prop.setStatus(false);

			int imgList = mPropImg.insertImagesToList(getProp());

			if (imgList < 1) {
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_ERROR, "Publish denied",
						"You need to upload atleast one image!"));
				return "failed";
			}

			propSaleRe.addNewPropForSale(getProp());
			propSaleRe.addNewPropForSaleImages(getProp(),
					mPropImg.getProp_img());
			mPropImg.setProp_img(new ArrayList());
			setSuccess(true);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Insert Successful!"));
			return "ok";

		} catch (Exception ex) {
			ex.printStackTrace();
			setSuccess(false);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Insert Failed!"));
			return "failed";
		}

	}

	public String addNewRoomForRent() throws Exception {
		try {

			HttpServletRequest req = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();
			MPropertyImage mPropImg = (MPropertyImage) req.getSession()
					.getAttribute("mPropertyImage");
			Context ctx = new InitialContext();
			RoomForRentBeanRemote roomRentRe = (RoomForRentBeanRemote) ctx
					.lookup("MySweetHome/RoomForRentBean/remote");

			roomRent.setPropID(getGenRID());

			prop.setRoomForRent(getRoomRent());

			prop.setPropID(getGenRID());
			prop.setAccount(getCurrentAccount());
			prop.setPostDate(getToday());
			prop.setStatus(false);

			int imgList = mPropImg.insertImagesToList(getProp());

			if (imgList < 1) {
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_ERROR, "Publish denied",
						"You need to upload atleast one image!"));
				return "failed";
			}

			roomRentRe.addNewRoomForRent(getProp());
			roomRentRe.addNewRoomForRentImages(getProp(),
					mPropImg.getProp_img());
			mPropImg.setProp_img(new ArrayList());
			setSuccess(true);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Insert Successful!"));
			return "ok";

		} catch (Exception ex) {
			ex.printStackTrace();
			setSuccess(false);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Insert Failed!"));
			return "failed";
		}

	}

	public void setProp(Property prop) {
		this.prop = prop;
	}

	public Property getProp() {
		return prop;
	}

	public void setPropRent(PropForRent propRent) {
		this.propRent = propRent;
	}

	public PropForRent getPropRent() {
		return propRent;
	}

	public void setPropSale(PropForSale propSale) {
		this.propSale = propSale;
	}

	public PropForSale getPropSale() {
		return propSale;
	}

	public void setRoomRent(RoomForRent roomRent) {
		this.roomRent = roomRent;
	}

	public RoomForRent getRoomRent() {
		return roomRent;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setCurrentAccount(Account currentAccount) {
		this.currentAccount = currentAccount;
	}

	public Account getCurrentAccount() throws Exception {
		HttpServletRequest req = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Object obj = req.getSession().getAttribute("mAccount");
		currentAccount = ((com.project.tructc.MBean.MAccount) obj).getAcc();
		return currentAccount;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public Date getToday() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		today = java.sql.Date.valueOf(dateFormat.format(cal.getTime()));
		return today;
	}

	public void setGenPID(String genPID) {
		this.genPID = genPID;
	}

	public String getGenPID() throws Exception {
		InitialContext ctx = new InitialContext();
		genPID = "";
		PropForRentBeanRemote propRentRe = (PropForRentBeanRemote) ctx
				.lookup("MySweetHome/PropForRentBean/remote");
		genPID = propRentRe.getNewID();
		return genPID;
	}

	public void setGenSID(String genSID) {
		this.genSID = genSID;
	}

	public String getGenSID() throws Exception {
		InitialContext ctx = new InitialContext();
		genSID = "";
		PropForSaleBeanRemote propSaleRe = (PropForSaleBeanRemote) ctx
				.lookup("MySweetHome/PropForSaleBean/remote");
		genSID = propSaleRe.getNewID();
		return genSID;
	}

	public void setGenRID(String genRID) {
		this.genRID = genRID;
	}

	public String getGenRID() throws Exception {
		InitialContext ctx = new InitialContext();
		genRID = "";
		RoomForRentBeanRemote roomRentRe = (RoomForRentBeanRemote) ctx
				.lookup("MySweetHome/RoomForRentBean/remote");
		genRID = roomRentRe.getNewID();
		return genRID;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int getTotalProperty() throws Exception {
		InitialContext ctx = new InitialContext();
		PropertyBeanRemote propRe = (PropertyBeanRemote) ctx
				.lookup("MySweetHome/PropertyBean/remote");
		List<Property> result = propRe.getAllPropFromAcc(getCurrentAccount());
		totalProperty = result.size();
		return totalProperty;
	}

	public void setPublishability(boolean publishability) {
		this.publishability = publishability;
	}

	public boolean isPublishability() throws Exception {
		if (getTotalProperty() >= 1
				&& getCurrentAccount().getIsVerified() == false) {
			publishability = false;
		} else
			publishability = true;
		return publishability;
	}

}
